package org.compiere.process;

import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.Calendar;
import java.util.logging.Level;

import org.adempiere.exceptions.AdempiereException;
import org.compiere.model.MBPartner;
import org.compiere.model.Mhrmabsence;
import org.compiere.model.Mhrmillnessline;
import org.compiere.model.Mhrmilness;
import org.compiere.model.Mhrmsettax;
import org.compiere.model.X_C_BPartner;
import org.compiere.model.X_C_Period;
import org.compiere.model.X_C_Tax;
import org.compiere.model.X_hrm_absence;
import org.compiere.model.X_hrm_set_tax;
import org.compiere.model.X_hrm_settingsPayments;
import org.compiere.util.Env;
import org.compiere.util.Msg;
import org.eevolution.model.X_HR_Employee;
import org.joda.time.DateTime;

import extend.org.compiere.hrm.AverageCalc;
import extend.org.compiere.hrm.AverageSalary;
import extend.org.compiere.hrm.TaxCompute;
import extend.org.compiere.hrm.TaxVariable;
import extend.org.compiere.hrm.TypePayment;
import extend.org.compiere.utils.Util;


public class GenerateIllnessSalary extends SvrProcess {
	
	
	private Timestamp dateFrom;
	private Timestamp sdt;
	private Timestamp edt;
	private int C_BPartner_ID = 0;
	private int HR_Block_ID  = 0;
	private int HR_Department_ID  = 0;	
	double SUM = 0;
	double total = 0;
	Calendar sMonth = Calendar.getInstance();
	Calendar eMonth = Calendar.getInstance();
	X_HR_Employee he = null;
	
	/**	Employee List	*/
	private MBPartner[] m_lines;
		
	/** Absence */
//	private Mhrmabsence[] illtype = null;
	
	private Mhrmabsence illtype = null;
	/** Illness */
	private Mhrmilness illpayment = null;
	private Mhrmillnessline illine = null;
	
	/** Tax */
	private TaxVariable taxcomp = null;
	
	/** Average */
	private AverageSalary average = null;
	
	/** */
	String closeTax = "N";
			
	@Override
	protected void prepare() {
		ProcessInfoParameter[] para = getParameter();
		for (int i = 0; i < para.length; i++)
		{
				String name = para[i].getParameterName();
				if (name.equals("CloseTax"))
					closeTax = para[i].getParameter().toString();
				else
					log.log(Level.INFO, "Unknown Parameter: " + name);			
		}	
		
		illpayment = new Mhrmilness(getCtx(), getRecord_ID(), null);
	}

	@Override
	protected String doIt() throws Exception {
	
		C_BPartner_ID = illpayment.getC_BPartner_ID();
		HR_Block_ID = illpayment.getHR_Block_ID();
		HR_Department_ID = illpayment.getHR_Department_ID();
		dateFrom = illpayment.getDateFrom();
		
		// Get last day of month				
		DateTime enddt = new DateTime(dateFrom.getTime());
		DateTime lastDayOfMonth = enddt.dayOfMonth().withMaximumValue();
		dateFrom = new Timestamp(lastDayOfMonth.getMillis());	
				
		MBPartner bp = new MBPartner(getCtx(), illpayment.getC_BPartner_ID(), null);
		m_lines = bp.getPartnerEmployeeDate(C_BPartner_ID, HR_Block_ID, HR_Department_ID, dateFrom, true);
		
		for(MBPartner b: m_lines){
			he = X_HR_Employee.getHR_Employee(b.getC_BPartner_ID(),null);
			if (he.getC_BPartner_ID() == 0)
				throw new AdempiereException ("Info about employee is empty");
			int days = Mhrmillnessline.getillDay(b.getC_BPartner_ID(), dateFrom, X_hrm_absence.HRM_ABSENCETYPE_Illness);
			if (days != 0){
				illine = Mhrmillnessline.getIllnessRec(b.getC_BPartner_ID(), dateFrom);
				if (illine == null)
					illine = new Mhrmillnessline(getCtx(), 0, null);
				illine.sethrm_illness_ID(illpayment.gethrm_illness_ID());
				illine.setC_BPartner_ID(b.getC_BPartner_ID());
				illine.setHR_Department_ID(he.getHR_Department_ID());
				illine.setHR_Job_ID(he.getHR_Job_ID());
				sdt = Mhrmillnessline.getMinDate(b.getC_BPartner_ID(), dateFrom, X_hrm_absence.HRM_ABSENCETYPE_Illness);
				illine.setStartDate(sdt);
				edt = Mhrmillnessline.getMaxDate(b.getC_BPartner_ID(), dateFrom, X_hrm_absence.HRM_ABSENCETYPE_Illness);
				illine.setEndDate(edt);					
				illine.sethrm_days(days);
				illine.sethrm_absenceType(X_hrm_absence.HRM_ABSENCETYPE_Illness);
					 	 
				// The algorithm illness salary					
				average = AverageCalc.getAvarage(b.getC_BPartner_ID(), dateFrom);
				illine.setDateFrom(average.getSTARTDATE());
				illine.setDateTo(average.getENDDATE());
				total = average.getRESULT();
				// Total equal Average Salary * days
				SUM = total * days;						 			    
				// Not over 15 MZP
				//double taxMRP = X_C_Tax.getRateName("MRP", dateFrom);
				BigDecimal taxMRP = Mhrmsettax.getRateValue(X_hrm_set_tax.HRM_TAXTYPE_MRP, dateFrom);
				if (SUM > taxMRP.doubleValue() * 15)
					SUM = taxMRP.doubleValue() * 15;
				 
				/** Generate employee tax */
				taxcomp =  TaxCompute.GetTax(b.getC_BPartner_ID(), SUM, dateFrom, TypePayment.ILLNESS);
				illine.sethrm_amountillness(new BigDecimal(total));
				illine.sethrm_IPN(new BigDecimal(taxcomp.getIPN()));
				illine.sethrm_OPV(new BigDecimal(taxcomp.getOPV()));
				illine.sethrm_total(new BigDecimal(taxcomp.getTOTAL()+taxcomp.getIPN()+taxcomp.getOPV()));
				// Set social tax variables
				illine.sethrm_COtch(new BigDecimal(taxcomp.getCOTCH()));
				illine.sethrm_CNalog(new BigDecimal(taxcomp.getCNALOG()));
				if (closeTax.equals("Y"))
				   	illine.setProcessed(true);				    
				    illine.saveEx();   
				 }
		}
		
		if (closeTax.equals("Y")){
		    illpayment.setProcessed(true);
		    illpayment.saveEx();
		}
		
		return Msg.translate(Env.getCtx(), "Success");
	}

}
